﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace ButtonTree2
{
    public static class AlignUtils
    {
        public static Point GetAlignedPosition(Rectangle rectArea, int width, int height, ContentAlignment align, Padding margin)
        {
            if (margin == null)
            {
                margin = new Padding(0);
            }

            int x = 0, y = 0;

            switch(align)
            {
                case ContentAlignment.TopLeft:
                case ContentAlignment.MiddleLeft:
                case ContentAlignment.BottomLeft:
                    x = rectArea.Left + margin.Left;
                    break;
                case ContentAlignment.TopCenter:
                case ContentAlignment.MiddleCenter:
                case ContentAlignment.BottomCenter:
                    x = rectArea.Left + (rectArea.Width - width) / 2;
                    break;
                case ContentAlignment.TopRight:
                case ContentAlignment.MiddleRight:
                case ContentAlignment.BottomRight:
                    x = rectArea.Right - margin.Right - width; ;
                    break;
            }

            switch(align)
            {
                case ContentAlignment.TopLeft:
                case ContentAlignment.TopCenter:
                case ContentAlignment.TopRight:
                    y = rectArea.Top + margin.Top;
                    break;

                case ContentAlignment.MiddleLeft:
                case ContentAlignment.MiddleCenter:
                case ContentAlignment.MiddleRight:
                    y = rectArea.Top + (rectArea.Height - height) / 2;
                    break;

                case ContentAlignment.BottomLeft:
                case ContentAlignment.BottomCenter:
                case ContentAlignment.BottomRight:
                    y = rectArea.Bottom - margin.Bottom - height;
                    break;
            }

            return new Point(x, y);
        }
    }
}
